#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
   @Author: 叶建伟
   @Email: yejw46507@hundsun.com
   @Time: 2022/6/14 19:32
   @File: suspicious_rule_config.py
"""
from time import sleep

from qturboframe.lib.utils.by import By
from BladeFerm.hui2_app import FE
from BladeFerm.Lib.Common import logger, data_processing
from BladeFerm.hui12_common.check_data import check_data_pattern
from 基础公共脚本.步骤检查_hui2 import check_ele_text
from 线下客户管理.线下客户共用脚本.open_menu import open_menu


@data_processing
def suspicious_trade_rule_modify(input_data=None, ui_expect=None):
    # 打开菜单
    open_menu({'子系统名称': '反洗钱系统', '菜单名称': '可疑交易管理/可疑交易规则'})
    # 选择规则
    select = FE.select(idname={'xpath': '//label[contains(text(),"可疑规则")]//following-sibling::div'})
    select.set_value_for_single(input_data.get('可疑规则'))
    # 修改基础信息
    if input_data.get('基础信息', None):
        modify_base_info(input_data.get('基础信息'))
        sleep(1)
        check_modify_result(input_data.get('基础信息'))
    # 修改配置信息
    if input_data.get('参数配置', None):
        modify_config_info(input_data.get('参数配置'))
        check_modify_result(input_data.get('参数配置'))
    # 检查期望
    check_ele_text(ui_expect)


def button_option(base='基础信息', option='修改'):
    target = FE.list(idname={By.XPATH: f'//h2[@class="base-title" and contains(text(), "{base}")]'})
    target.foud_target(idname={By.XPATH: './span'})
    target.click(option)


def modify_base_info(input_data=None):
    button_option("基础信息", "修改")
    if input_data.get('是否启用', None):
        select1 = FE.select(idname={'xpath': '//th[contains(text(),"是否启用")]//following-sibling::'
                                             'td//div[contains(@class,"h-select-single")]'})
        select1.set_value_for_single(input_data.get('是否启用'))
    if input_data.get('可疑特征', None):
        select2 = FE.select(idname={'xpath': '//th[contains(text(),"可疑特征")]//following-sibling::'
                                             'td//div[contains(@class,"h-select-single")]'})
        select2.set_value_for_single(input_data.get('可疑特征'))
    if input_data.get('描述', None):
        input1 = FE.list(idname={'xpath': '//th[contains(text(),"描述")]//following-sibling::'
                                          'td//div[contains(@class,"h-input-wrapper")]/textarea'})
        input1.element.clear()
        input1.element.send_keys(input_data.get('描述'))
    button_option("基础信息", "保存")


def modify_config_info(input_data=None):
    button_option("参数配置", "修改")
    if input_data.get('基金类型', None):
        select = FE.select(idname={By.XPATH: '//td[contains(text(),"交易包含的基金类型")]//following-sibling::'
                                             'td/div/div[contains(@class,"h-select-multiple")]'})
        select.set_value_for_multiple(input_data.get('基金类型').split(','))
    if input_data.get("销售商", None):
        select = FE.select(idname={By.XPATH: '//td[contains(text(),"交易排除的销售商")]//following-sibling::'
                                             'td/div/div[contains(@class,"h-select-multiple")]'})
        select.set_value_for_multiple(input_data.get('基金类型').split(','))
    button_option("保存")


def check_modify_result(ui_except=None):
    if ui_except:
        for key, value in ui_except.items():
            target = FE.list(idname={By.XPATH: f'//th[contains(text(),{key})]//following-sibling::td/span'})
            logger.info(f'text1:{target.element.text}')
            cur_result = target.element.text
            check_data_pattern(value, cur_result)
    else:
        logger.warning(f'Skip check!')